// serve.prehandle.js
const portfinder = require("portfinder");
const isDevEnv = process.env.NODE_ENV !== "production";

module.exports = (api, options) => {
    api.registerCommand("serve:prehandle", async(args) => {
        if (isDevEnv) {
            portfinder.basePort = process.env.PORT || 8081;
            console.log(portfinder.basePort, "端口");
            // 把获取到的可用端口存到 process 对象上
            process.finderPort = await portfinder.getPortPromise();
        }
        args.port = process.finderPort;
        await api.service.run("serve", args);
    });
};

module.exports.defaultModes = {
    "serve:prehandle": "development",
};